﻿using System;
using System.Data;
using System.IO;

/// <summary>
/// DataSet扩展类
/// </summary>
public static class DataSetExtender
{  //增加string的方法, CheckCnCharacter

    /// <summary>
    /// 数据集转化为json字符
    /// </summary>
    /// <param name="_ds">数据集</param>
    /// <param name="path">路径 示例/: AppDomain .CurrentDomain.BaseDirectory/Directory/FileName.json</param>
    /// <param name="totalCount">总条数</param>
    /// <returns>json字符</returns>
    public static void ToWriteJson(this DataSet _ds, int totalCount, string path)
    {
        string jsonStr = "";
        jsonStr = "{TotalCount:\"" + totalCount + "\"," + _ds.Tables[0].TableName + ":[";

        for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
        {
            if (i + 1 > _ds.Tables[0].Rows.Count)
            {
                continue;
            }
            string row = @"{";
            for (int j = 0; j < _ds.Tables[0].Columns.Count; j++)
            {
                if (j == 0)
                {
                    row += string.Format("{0}:\"{1}\"", _ds.Tables[0].Columns[j].ColumnName, _ds.Tables[0].Rows[i][j].ToString());
                }
                else
                {
                    row += string.Format(",{0}:\"{1}\"", _ds.Tables[0].Columns[j].ColumnName, _ds.Tables[0].Rows[i][j].ToString());
                }
            }
            row += "}";
            if (i == 0)
            {
                jsonStr += row;
            }
            else
            {
                jsonStr += "," + row;
            }
        }
        jsonStr += "]";
        jsonStr += "}";

        if (File.Exists(path))
        {
            FileStream fs = File.Create(path);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(jsonStr);
            sw.Close();
            fs.Close();
        }


    }
    /// <summary>
    /// 数据集转化为json字符
    /// </summary>
    /// <param name="_ds">数据集</param>
    /// <param name="totalCount">总条数</param>
    /// <returns>json字符</returns>
    public static string ToJson(this DataSet _ds, int totalCount)
    {
        string jsonStr = "";
        jsonStr = "{TotalCount:\"" + totalCount + "\"," + _ds.Tables[0].TableName + ":[";

        for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
        {
            if (i + 1 > _ds.Tables[0].Rows.Count)
            {
                continue;
            }
            string row = @"{";
            for (int j = 0; j < _ds.Tables[0].Columns.Count; j++)
            {
                if (j == 0)
                {
                    row += string.Format("{0}:\"{1}\"", _ds.Tables[0].Columns[j].ColumnName, _ds.Tables[0].Rows[i][j].ToString());
                }
                else
                {
                    row += string.Format(",{0}:\"{1}\"", _ds.Tables[0].Columns[j].ColumnName, _ds.Tables[0].Rows[i][j].ToString());
                }
            }
            row += "}";
            if (i == 0)
            {
                jsonStr += row;
            }
            else
            {
                jsonStr += "," + row;
            }
        }
        jsonStr += "]";
        jsonStr += "}";


        return jsonStr;

    }

    /// <summary>
    /// 数据集转化为json字符
    /// </summary>
    /// <param name="_ds">数据集</param>
    /// <param name="totalCount">总条数</param>
    /// <returns>json字符</returns>
    public static string ToJson(this DataSet _ds)
    {
        string jsonStr = "";
        int totalCount = 0;
        if (_ds.Tables.Count > 1)
        {
            totalCount = Convert.ToInt32(_ds.Tables[1].Rows[0][0]);
        }
        jsonStr = "{TotalCount:\"" + totalCount + "\"," + _ds.Tables[0].TableName + ":[";

        for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
        {
            if (i + 1 > _ds.Tables[0].Rows.Count)
            {
                continue;
            }
            string row = @"{";
            for (int j = 0; j < _ds.Tables[0].Columns.Count; j++)
            {
                if (j == 0)
                {
                    row += string.Format("{0}:\"{1}\"", _ds.Tables[0].Columns[j].ColumnName, _ds.Tables[0].Rows[i][j].ToString());
                }
                else
                {
                    row += string.Format(",{0}:\"{1}\"", _ds.Tables[0].Columns[j].ColumnName, _ds.Tables[0].Rows[i][j].ToString());
                }
            }
            row += "}";
            if (i == 0)
            {
                jsonStr += row;
            }
            else
            {
                jsonStr += "," + row;
            }
        }
        jsonStr += "]";
        jsonStr += "}";


        return jsonStr;

    }
    /// <summary>
    /// 数据集转化为json字符[二维数组格式]
    /// </summary>
    /// <param name="_ds">数据集</param>
    /// <param name="totalCount">总条数</param>
    /// <returns>json字符</returns>
    public static string ToArrayJson(this DataSet _ds)
    {
        string jsonStr = "";
        int totalCount = 0;
        if (_ds.Tables.Count > 1)
        {
            totalCount = Convert.ToInt32(_ds.Tables[1].Rows[0][0]);
        }
        jsonStr = "{TotalCount:\"" + totalCount + "\"," + _ds.Tables[0].TableName + ":[";

        for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
        {
            if (i + 1 > _ds.Tables[0].Rows.Count)
            {
                continue;
            }
            string row = @"[";
            for (int j = 0; j < _ds.Tables[0].Columns.Count; j++)
            {
                if (j == 0)
                {
                    row += string.Format("{0}", _ds.Tables[0].Rows[i][j].ToString());
                }
                else
                {
                    row += string.Format(",{0}", _ds.Tables[0].Rows[i][j].ToString());
                }
            }
            row += "]";
            if (i == 0)
            {
                jsonStr += row;
            }
            else
            {
                jsonStr += "," + row;
            }
        }
        jsonStr += "]";
        jsonStr += "}";
        return jsonStr;
    }
}


